﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class NoteBLL
  {
    public List<Note> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Notes.OrderByDescending(p => p.CreateDate).ToList();
      }
    }

    public List<Note> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Notes.Where(p => p.IsEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
      }
    }
    

    //Get by id
    public Note GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Notes.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Note GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Notes.Where(p => p.Id == id && p.IsEnable == isEnable).FirstOrDefault();
      }
    }


    public Note GetActive()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Notes.Where(p => p.IsEnable == true).FirstOrDefault();
      }
    }
  
    //Enable
    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Note obj = data.Notes.Where(p => p.Id == id).FirstOrDefault();
        obj.IsEnable = !obj.IsEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Note obj = data.Notes.Where(p => p.Id == id).FirstOrDefault();
        obj.IsEnable = isEnable;

        data.SubmitChanges();
      }
    }

    public void DisEnableAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        List<Note> list = data.Notes.ToList();
        foreach (var item in list)
        {
          if (item.IsEnable == true)
            item.IsEnable = false;
        }
        data.SubmitChanges();
      }
    }

    //Add
    public void Add(Note obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Notes.InsertOnSubmit(obj);

        data.SubmitChanges();
      }
    }

    //Update
    public void Update(Note obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Note note = data.Notes.Where(p => p.Id == obj.Id).FirstOrDefault();

        note.Name = obj.Name;
        note.Name_En = obj.Name_En;
        note.Content = obj.Content;
        note.Content_En = obj.Content_En;
        note.CreateDate = obj.CreateDate;
        note.IsEnable = obj.IsEnable;

        data.SubmitChanges();
      }
    }

    //Delete
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Note obj = data.Notes.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.Notes.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }    
  }
}
